package net.asfun.jvalog.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.domain.Category;
import net.asfun.jvalog.http.RequestUtil;
import net.asfun.jvalog.view.PagerInfo;


public class CategoryAction extends BaseAction{
	
	/**
	 * list categories
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	public void index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int page = RequestUtil.getIntParam(req, "page");
		PagerInfo pager = new PagerInfo();
		pager.setIndex(page);
		List<Category> categories = Category.list(pager);
		this.assign("pager", pager);
		this.assign("categories", categories);
		this.render("admin/categories.html", resp);
	}
	
	/**
	 * del one or more categories
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for (int i=0; i<ids.length; i++) {
			Category.remove(ids[i]);
		}
		this.feedback("category.delete", req, resp);
	}
	
	public void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		this.render("admin/category.html", resp);
	}
	
	/**
	 * create a category
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = RequestUtil.getRequiredParam(req, "name");
		String slogan =  RequestUtil.getStringParam(req, "slogan", name);
		int order = RequestUtil.getIntParam(req, "order");
//		String parent = RequestUtil.getStringParam(req, "parent");
		Category category = new Category();
		category.setOrder(order);
		category.setName(name);
		category.setSlogan(slogan);
		category.save();
		this.feedback("category.add", req, resp);
	}
	
	/**
	 * update category info
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		String name = RequestUtil.getRequiredParam(req, "name");
		String slogan =  RequestUtil.getStringParam(req, "slogan", name);
		int order = RequestUtil.getIntParam(req, "order");
//		String parent = RequestUtil.getStringParam(req, "parent");
		Category category = Category.load(id);
		category.setName(name);
		category.setSlogan(slogan);
		category.setOrder(order);
		category.update();
		this.feedback("category.update", req, resp);
	}
	
	/**
	 * view category detail
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		this.assign("category", Category.load(id));
		this.render("admin/category.html", resp);
	}
}
